package com.luo.shop.controller;

import com.luo.shop.pojo.Address;
import com.luo.shop.pojo.User;
import com.luo.shop.service.AddressService;
import com.luo.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("address")
public class AddressController {
    @Autowired
    private AddressService addressService;
    @Autowired
    private UserService userService;

    @PostMapping("save")
    public String save(Address address,HttpSession session){
        User user =  (User)session.getAttribute("portalUser");
        address.setUserId(user.getId());
        addressService.save(address);
        session.setAttribute("address",addressService.queryByUid(user.getId()).get(0));
        return "redirect:/address";
    }

    @PostMapping("update")
    public String update(Address address, HttpSession session){
        User user =  (User)session.getAttribute("portalUser");
        address.setUserId(user.getId());
        addressService.update(address);
        session.setAttribute("address",addressService.queryByUid(user.getId()).get(0));
        return "redirect:/address";
    }

    @PostMapping("saveOrder")
    @ResponseBody
    public Integer saveOrder(Address address,HttpSession session){
        User user =  (User)session.getAttribute("portalUser");
        address.setUserId(user.getId());
        return addressService.save(address);
    }

    @PostMapping("updateOrder")
    @ResponseBody
    public Integer updateOrder(Address address,Long gid, Model model, HttpSession session){
        User user =  (User)session.getAttribute("portalUser");
        address.setUserId(user.getId());
        return  addressService.update(address);
    }
}
